/*
 *  Copyright 2019-2025 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.server.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import me.zhengjie.dao.ContainerMove;
import me.zhengjie.dao.dto.ContainerMoveQueryCriteria;
import me.zhengjie.mapper.ContainerMoveMapper;
import me.zhengjie.server.ContainerMoveService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.SnowIdUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * @author laoxu
 * @description 服务实现
 * @date 2025-06-27
 **/
@Service
@RequiredArgsConstructor
public class ContainerMoveServiceImpl extends ServiceImpl<ContainerMoveMapper, ContainerMove> implements ContainerMoveService {

    private final ContainerMoveMapper containerMoveMapper;

    @Override
    public PageResult<ContainerMove> queryAll(ContainerMoveQueryCriteria criteria, Page<Object> page) {
        return PageUtil.toPage(containerMoveMapper.findAll(criteria, page));
    }

    @Override
    public List<ContainerMove> queryAll(ContainerMoveQueryCriteria criteria) {
        return containerMoveMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ContainerMove resources) {
        containerMoveMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ContainerMove resources) {
        ContainerMove containerMove = getById(resources.getId());
        containerMove.copy(resources);
        containerMoveMapper.updateById(containerMove);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<String> ids) {
        containerMoveMapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<ContainerMove> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ContainerMove containerMove : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("操作人", containerMove.getName());
            map.put("动碰次数", containerMove.getCount());
            map.put("动碰时间", containerMove.getMoveDate());
            map.put("载具编码", containerMove.getContainerCode());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public ContainerMove findContainerMoveByCode(String containerCode) {
        LambdaQueryWrapper<ContainerMove> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ContainerMove::getContainerCode, containerCode);
        return containerMoveMapper.selectOne(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createContainerMove(String freezeName, String containerCode) {
        ContainerMove containerMove = this.findContainerMoveByCode(containerCode);
        if (containerMove == null) {
            containerMove = new ContainerMove()
                    .setId(String.valueOf(SnowIdUtil.nextId()))
                    .setCount(1)
                    .setMoveDate(new Timestamp(System.currentTimeMillis()))
                    .setContainerCode(containerCode)
                    .setName(freezeName);
        } else {
            containerMove
                    .setName(freezeName)
                    .setMoveDate(new Timestamp(System.currentTimeMillis()))
                    .setCount(containerMove.getCount() + 1);
        }
        this.saveOrUpdate(containerMove);
    }
}